package com.kyxx.mycommonproject.xianshimiaosha;

import android.graphics.Paint;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.kyxx.mycommonproject.R;
import com.kyxx.mycommonproject.base.BaseActivity0;

public class MiaoShaActivity extends BaseActivity0 {
    private int secondCount = 10800; // 倒计时3小时
    private RecyclerView rv;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_miao_sha);
    }

    @Override
    protected void findViewById() {
        rv = (RecyclerView) findViewById(R.id.rv);
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void processLogic() {
        initTimer();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rv.setLayoutManager(linearLayoutManager);
        MiaoShaAdapter miaoShaAdapter = new MiaoShaAdapter();
        rv.setAdapter(miaoShaAdapter);
    }

    class MiaoShaAdapter extends RecyclerView.Adapter<ViewHolder> {
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view =
                    LayoutInflater.from(parent.getContext()).inflate(R.layout.item_miaosha_hor, parent, false);
            ViewHolder holder = new ViewHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.tv_price_true.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG ); ;
        }

        @Override
        public int getItemCount() {
            return 10;
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_good;
        TextView tv_price;
        TextView tv_price_true;

        public ViewHolder(View view) {
            super(view);
            iv_good = (ImageView) view.findViewById(R.id.iv_good);
            tv_price = (TextView) view.findViewById(R.id.tv_price);
            tv_price_true = (TextView) view.findViewById(R.id.tv_price_true);
        }
    }

    /**
     * 倒计时
     */
    private void initTimer() {
        final TextView tvHour = (TextView) findViewById(R.id.tv_hour);
        final TextView tvMinute = (TextView) findViewById(R.id.tv_minute);
        final TextView tvSecond = (TextView) findViewById(R.id.tv_second);
        tvHour.post(new Runnable() {

            @Override
            public void run() {
                if (secondCount > 0) {

                    secondCount--;
                    int h = secondCount / 3600;
                    int m = secondCount / 60 % 60;
                    int s = secondCount % 60;
                    StringBuffer hour = new StringBuffer();
                    StringBuffer minute = new StringBuffer();
                    StringBuffer second = new StringBuffer();
                    if (h < 10) {
                        hour.append(0);
                    }
                    if (m < 10) {
                        minute.append(0);
                    }
                    if (s < 10) {
                        second.append(0);
                    }
                    hour.append(h);
                    minute.append(m);
                    second.append(s);
                    tvHour.setText(hour);
                    tvMinute.setText(minute);
                    tvSecond.setText(second);
                    tvHour.postDelayed(this, 1000);
                }
            }
        });
    }
}
